﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Input;
using iOSClub.Toolkit.WindowApp.Controls;
using iOSClub.Toolkit.WindowApp.ViewModels.Pages;

namespace iOSClub.Toolkit.WindowApp.Pages;

public partial class DocumentPage : UserControl
{
    public DocumentPage()
    {
        InitializeComponent();
    }

    private async void DoneClick(object? sender, RoutedEventArgs e)
    {

        if(DataContext is not DocumentPageModel model)return;
        
        var td = new TaskDialog
        {
            // Title property only applies on Windowed dialogs
            Title = "西建大iOS Club工具包",
            Header = "表格错误",
            Content = "内容没有填完!",
            IconSource = new SymbolIconSource { Symbol = Symbol.Save },
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        if(!await model.Done())
            await td.ShowAsync(true);
        else
        {
            td.Header = "生成完毕";
            td.Content = $"现在文件在{model.DocPath},是否打开?";
            await td.ShowAsync(true);
        }
    }

    private async void AddMemberClick(object? sender, RoutedEventArgs e)
    {
        var td = new TaskDialog
        {
            // Title property only applies on Windowed dialogs
            Title = "西建大iOS Club工具包",
            Header = "添加成员",
            Content = new AddMemberControl(),
            IconSource = new SymbolIconSource { Symbol = Symbol.Save },
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton,
                TaskDialogButton.CancelButton
            },
            XamlRoot = (Visual)VisualRoot!
        };

        td.Closing += (dialog, args) =>
        {
            if ((TaskDialogStandardResult)args.Result != TaskDialogStandardResult.OK) return;
            var result = (dialog.Content as AddMemberControl)!.Done();
            if (result == null)
            {
                args.Cancel = true;
                return;
            }
            if(DataContext is not DocumentPageModel model)return;
            model.Members.Add(result);
        };
        
        await td.ShowAsync();
    }
}